<%--
  @author Duminda Dharmakeerthi
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<script src="${pageContext.request.contextPath}/js/printer.js" type="text/javascript"></script>
<%--<link rel="stylesheet" href="${pageContext.request.contextPath}/css/printDetailForm.css" media="print"/>--%>

<script type="text/javascript">

    function changeMinHectare() {
        var minHe = 0,minAc = 0,minRo = 0,minPe = 0,minTemp = 0;
        var minCont = 2.47105381467;
        minHe = ($('#hectareId').val().length == 0) ? 0 : $('#hectareId').val();
        minAc = Math.floor(minHe * minCont);
        minTemp = (minHe * minCont - minAc) * 4;
        minRo = Math.floor(minTemp);
        minTemp = (minTemp - minRo) * 40;
        minPe = minTemp.toFixed(4);
        $("#minAcreId").text(minAc);
        $("#minRoodsId").text(minRo);
        $("#minPerchesId").text(minPe);
    }

    function fillSubDivision() {
        var totalSubDivisionInPerches = 0.0;
        totalSubDivisionInPerches = $('#minimumSubDivisionUnit').val();
        if (!totalSubDivisionInPerches.length) {
            totalSubDivisionInPerches = 0.0;
        }
        totalSubDivisionInPerches = parseFloat(totalSubDivisionInPerches);
        var minPe = totalSubDivisionInPerches % 40;
        var minRo = ((totalSubDivisionInPerches - minPe) / 40) % 4;
        var minAc = ((((totalSubDivisionInPerches - minPe) / 40) - minRo) / 4);
        var minHe = totalSubDivisionInPerches * 0.002529285264;
        $("#minAcreId").text(minAc);
        $("#minRoodsId").text(minRo);
        $("#minPerchesId").text(minPe.toFixed(4));
        $("#minHectareId").text(minHe.toFixed(4));
    }

    $(function () {
        fillSubDivision();
//        // $('#area').hide();
//        var hectare = $("#minimumSubDivisionUnit").val();
//        hectare = parseFloat(hectare);
//        if (hectare > 0) {
//            fillSubDivision();
//        }
    });

    $(document).ready(function() {
        $('#deed-tree').treegrid({
            title:$('#landGrantHistory').val(),
            nowrap: false,
            rownumbers: true,
            animate:true,
            collapsible:true,
            idField:'deed',
            treeField:'deed',
            frozenColumns:[
                [
                    {title:$('#grantHistory').val(),field:'deed',width:140,
                        formatter:function(value) {
                            return '<span style="color:red">' + value + '</span>';
                        }
                    }
                ]
            ],
            columns:[
                [
                    {field:'name',title:$('#reference').val(),width:537}
                ]
            ]
        });
        loadAlienationTree();
    });


    function loadAlienationTree() {
        var grantId = $('#grantId').attr('value');
        var parent;
        $.getJSON('${pageContext.request.contextPath}/common/AlienationHistoryLookupService', {grantId:grantId}, function(list) {
            var AList = list.alienationList;
            var i = AList.length - 1;
            for (i; i > -1; i--) {
                if (AList[i].immediateParent == null) {
                    parent = null;
                } else {
                    parent = $('#deed-tree').treegrid('find', getImmediateParentId(AList[i]));
                }

                var id = AList[i].id;
                var data = [
                    {
                        deed: id,
                        name: AList[i].referenceNo
                    }
                ];
                addAlienation(parent, data);
            }
        });
    }

    function getImmediateParentId(obj) {

        var p;
        $.each(obj, function(key, element) {
            if (key == "immediateParent") {
                p = element.id;
            }
        });
        return p;
    }

    function addAlienation(parent, data) {

        $('#deed-tree').treegrid('append', {
            parent: (parent ? parent.deed : null),
            data: data
        });
    }


</script>

<s:include value="../../printCaller.jsp"/>

<div id="printDivId" class="form-outer">
    <h2><s:label value="%{getText('detailed_land_info.label')}"/> - <s:label
            value="%{getText('land_grants.label')}"/></h2>
    <hr/>
    <s:if test="hasActionErrors() || hasActionMessages()">
        <div id="message-box" class="message-box">
            <div class="action-msg"><s:actionmessage/></div>
            <div class="action-err"><s:actionerror/></div>
        </div>
    </s:if>
    <div id="deedPrint">
        <table id="deed-tree" class="width-100">
            <%-- Deed tree will be here --%>
        </table>
    </div>
    <s:form id="gov-land-detail-info-form" method="POST">
        <div id="tabs">
            <%--<div id="printbtn" class="form-button-div right-align">
                    &lt;%&ndash;<input type="button" value="Print" id="print"  class="print-btn">&ndash;%&gt;
                <input type="button" value="Print" onclick="printGrantLongPage();">
                <input class="printButton" type="button" value="Back" onclick="goBack();">

            </div>--%>


            <div id="land-details">
                <h3><s:label value="%{getText('land_details.label')}"/></h3>
                <table class="width-100">

                    <s:include value="../landDetailsReport.jsp"/>


                </table>
                <hr/>
            </div>


            <div id="land-grants-details">
                <table class="width-100">
                    <h3><s:label
                            value="%{getText('land_grants_details.label')}"/></h3>
                    <tr>
                        <td><s:label value="%{getText('grant_no.label')}"/>/<s:label
                                value="%{getText('deed_no.label')}"/></td>
                        <td><s:label name="alienation.referenceNo"/></td>
                        <td><s:label value="%{getText('date_of_approved.label')}"/></td>
                        <td><s:label name="alienation.issueDate"/></td>
                    </tr>
                    <tr>
                        <td><s:label value="%{getText('folio_no.label')}"/></td>
                        <td><s:label id="folioNo" name="alienation.folioNumber" cssClass="width-200-px"/></td>
                        <td><s:label value="%{getText('folio_date.label')}"/></td>
                        <td><s:label id="folioDate" name="alienation.folioDate" cssClass="width-200-px"/></td>
                    </tr>
                    <tr>
                        <td><s:label value="%{getText('folio_location.label')}"/></td>
                        <td colspan="3"><s:label id="folioLocation" name="alienation.folioLocation"
                                                 cssClass="width-595-px" rows="1"/></td>
                    </tr>
                    <tr>
                        <td><s:label value="%{getText('developed_status.label')}"/></td>
                        <td colspan="3" class="left-align"><s:label
                                name="alienation.developmentState"
                                cssClass="width-595-px"/></td>
                    </tr>
                    <tr>
                        <td><s:label value="%{getText('minimum_sub_division_unit.label')}"/></td>
                        <td colspan="3" class="left-align"><s:label value="%{getText('hectare.label')} :"/>
                            <s:label id="minHectareId" name="minHectare"/></td>
                        <s:hidden id="minimumSubDivisionUnit" name="alienation.land.minimumSubDivisionUnit"/>
                    </tr>
                    <tr>
                        <td></td>
                        <td colspan="3" class="left-align"><s:label value="%{getText('acre.label')} :"/>
                            <s:label id="minAcreId" name="acre"/>
                            &nbsp&nbsp&nbsp<s:label value="%{getText('roods.label')} :"/>
                            <s:label id="minRoodsId" name="roods"/>
                            &nbsp&nbsp&nbsp<s:label
                                    value="%{getText('perches.label')} :"/> <s:label id="minPerchesId"
                                                                                     name="perches"/></td>
                        </td>
                    </tr>
                    <tr>
                        <td><s:label value="%{getText('minimum_fraction.label')}"/></td>
                        <td colspan="3"><s:label name="alienation.land.minimumFraction" cssClass="width-595-px"/></td>
                    </tr>
                    <tr>
                        <td><s:label value="%{getText('comment_about_alienation.label')}"/></td>
                        <td colspan="3"><s:label name="alienation.alienationComment" cssClass="width-595-px"/></td>
                    </tr>
                </table>
                <hr/>
            </div>
            <s:if test="alienation.mortgage!=null">
                <div id="mortgage-details">
                    <h3><s:label value="%{getText('mortgage_details.label')}"/></h3>
                    <table class="width-100">
                        <tr>
                            <td><s:label value="%{getText('name_of_bank_institute.label')}"/></td>
                            <td colspan="3"><s:label name="alienation.mortgage.nameOfBank"
                                                     cssClass="width-610-px"/></td>
                        </tr>
                        <tr>
                            <td><s:label value="%{getText('address_of_bank_institute.label')}"/></td>
                            <td colspan="3"><s:label name="alienation.mortgage.addressOfBank"
                                                     cssClass="width-610-px"/></td>
                        </tr>
                        <tr>
                            <td><s:label value="%{getText('started_date.label')}"/></td>
                            <td><s:label name="alienation.mortgage.startedDate"/></td>
                            <td><s:label value="%{getText('ending_date.label')}"/></td>
                            <td><s:label name="alienation.mortgage.endingDate"/></td>
                        </tr>
                        <tr>
                            <td><s:label value="%{getText('mortgage_conditions.label')}"/></td>
                            <td colspan="3"><s:label name="alienation.mortgage.conditions"
                                                     cssClass="width-610-px"/></td>
                        </tr>
                    </table>
                    <hr/>
                </div>
                <s:hidden name="alienation.mortgage.id"/>
            </s:if>


            <div id="personal-details">
                <h3><s:label value="%{getText('personal_details.label')}"/></h3>
                <table class="width-100">
                    <s:include value="../personalDetailsReport.jsp"/>
                </table>
                <hr/>
                <s:hidden name="alienation.id"/>
                <s:hidden name="contact.id"/>
                <s:hidden name="holder.id"/>
                <s:hidden name="mortgage.id"/>

            </div>


            <s:if test="land.occupiers.size() >0">
            <div id="occupier-details">
                <h3><s:label value="%{getText('occupier_details.label')}"/></h3>
                <table class="width-100">
                    <s:include value="../occupierDetailsReport.jsp"/>
                </table>
                <hr/>
            </div>

        </s:if>

        </div>

    </s:form>
</div>
<s:hidden id="grantId" name="grantId"/>
<s:hidden id="pls_enter_reference_no" value="%{getText('pls_enter_reference_no')}"/>
<s:hidden id="pls_enter_name_of_the_land" value="%{getText('pls_enter_name_of_the_land')}"/>
<s:hidden id="pls_enter_plan_no" value="%{getText('pls_enter_plan_no')}"/>
<s:hidden id="pls_enter_lot_no" value="%{getText('pls_enter_lot_no')}"/>
<s:hidden id="pls_enter_valid_no" value="%{getText('pls_enter_valid_no')}"/>
<s:hidden id="pls_select_gn_division" value="%{getText('pls_select_gn_division')}"/>


<s:hidden id="landGrantHistory" value="%{getText('land_grant_history.label')}"/>
<s:hidden id="grantHistory" value="%{getText('grant_history.label')}"/>
<s:hidden id="reference" value="%{getText('reference.label')}"/>
<s:hidden id="transfer" value="%{getText('transfer.label')}"/>